package edu.depaul.se.courseregistration.mdb.pubsub;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 * CourseUpdateTopicPublisher class publishes messages to jms/javaee6/Topic
 * topic. <br> Once any course setting changes, related session bean will call
 * an object of RemindCourseUpdate class, <br> which references
 * CourseUpdateTopicPublisher.
 *
 * @author xchen
 */
public class CourseUpdateTopicPublisher {

    Context jndiContext;
    ConnectionFactory connectionFactory;
    Topic topic;
    Connection connection;
    Session session;
    MessageProducer producer;
    TextMessage message;
    public static final String CONFIRMATION = "CONFIRMATION";
    public static final String ERROR = "ERROR";

    public CourseUpdateTopicPublisher() {
        jndiContext = null;
        connectionFactory = null;
        topic = null;
        connection = null;
        session = null;
        producer = null;
        message = null;
    }

    public void sendMessage(String msg) {

        // Get JNDI context
        try {
            jndiContext = new InitialContext();
        } catch (NamingException ex) {
            Logger.getLogger(CourseUpdateTopicPublisher.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("JNDI API lookup failed: " + ex.toString());
        }

        // Looks up the administered objects
        try {
            connectionFactory = (ConnectionFactory) jndiContext.lookup("jms/javaee6/ConnectionFactory");
            topic = (Topic) jndiContext.lookup("jms/javaee6/Topic");
        } catch (NamingException ex) {
            Logger.getLogger(CourseUpdateTopicPublisher.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("JNDI API lookup failed: " + ex.toString());
        }
        try {
            // Connect to queue to send message
            connection = connectionFactory.createConnection();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            producer = session.createProducer(topic);

            // Send message
            message = session.createTextMessage();
            message.setText(msg);
            System.out.println("New message publish to jms/javaee6/Topic : " + msg);
            producer.send(message);
            System.out.println("Message was sent");
        } catch (JMSException ex) {
            Logger.getLogger(CourseUpdateTopicPublisher.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("Exception occurred during sending the message: " + ex.toString());
        } finally {
            if (connection != null) {
                try {
                    // Clean up
                    connection.close();
                } catch (JMSException ex) {
                    Logger.getLogger(CourseUpdateTopicPublisher.class.getName()).log(Level.SEVERE, null, ex);
                    System.out.println("Failed to close connection: " + ex.toString());
                }
            }
        }
    }
}
